package com.liao.badminton.handler.security;


import com.liao.badminton.code.myenum.RoleCodeEnum;
import com.liao.badminton.context.BaseContextHandler;
import com.liao.badminton.dto.UserGroupRefDto;
import com.liao.badminton.entity.SystemUsers;
import com.liao.badminton.service.SystemRoleService;
import com.liao.badminton.service.SystemUsersService;
import com.liao.badminton.service.UserGroupRefService;
import com.liao.badminton.vo.SystemUsersVo;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MyUserDao implements UserDetailsService {
    @Resource
    private SystemUsersService systemUsersService;
//    @Resource
//    private SystemRoleService systemRoleService;
//    @Resource
//    private UserGroupRefService userGroupRefService;

    @Override
    public UserDetails loadUserByUsername(String username) {


        // 查询用户信息)
        // 查询用户信息
        SystemUsersVo detail = systemUsersService.findByName(username);
        UserDetails build = null;
        if (detail != null) {
            BaseContextHandler.setUserName(detail.getUsername());
            String[] array = new String[]{};
//            //不需要找了，权限的时候会查的
//            if (array.length == 0) {
//                //查询用户是哪个群登录
////                array = systemRoleService.findCodeByUserName(username).toArray(new String[0]);
//            }
//            if (array.length == 0) {
                array = new String[]{RoleCodeEnum.GROUP_MEMBER.getCode()};
//            }
            build = User.withUsername(detail.getUsername()).password((detail.getPassword())).authorities(array).build();
        } else {
            throw new UsernameNotFoundException("用户名错误");
        }
        return build;
    }

}
